package com.zxl.grocery.modular.crawler.controller;

import com.zxl.grocery.common.annotion.Permission;
import com.zxl.grocery.common.annotion.log.BussinessLog;
import com.zxl.grocery.common.constant.Const;
import com.zxl.grocery.common.constant.Dict;
import com.zxl.grocery.common.controller.BaseController;
import com.zxl.grocery.common.exception.BizExceptionEnum;
import com.zxl.grocery.common.exception.BussinessException;
import com.zxl.grocery.common.persistence.dao.CrawlerNewsMapper;
import com.zxl.grocery.common.persistence.model.CrawlerNews;
import com.zxl.grocery.core.log.LogObjectHolder;
import com.zxl.grocery.core.shiro.ShiroKit;
import com.zxl.grocery.core.util.ToolUtil;
import com.zxl.grocery.modular.crawler.dao.NewsMgrDao;
import com.zxl.grocery.modular.crawler.warpper.NewsMgrWarpper;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 新闻管理控制器
 *
 * @author zxl
 * @Date 2017-08-06 01:23:44
 */
@Controller
@RequestMapping("/newsMgr")
public class NewsMgrController extends BaseController {

    private String PREFIX = "/crawler/newsMgr/";

    @Resource
    private NewsMgrDao newsMgrDao;

    @Resource
    private CrawlerNewsMapper crawlerNewsMapper;


    /**
     * 跳转到新闻管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "newsMgr.html";
    }

    /**
     * 跳转到添加新闻管理
     */
    @RequestMapping("/newsMgr_add")
    public String newsMgrAdd() {
        return PREFIX + "newsMgr_add.html";
    }

    /**
     * 跳转到修改新闻管理
     */
    @RequestMapping("/newsMgr_update/{newsMgrId}")
    public String newsMgrUpdate(@PathVariable Integer newsMgrId, Model model) {
        if(ToolUtil.isEmpty(newsMgrId)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CrawlerNews crawlerNews = crawlerNewsMapper.selectById(newsMgrId);
        model.addAttribute("crawlerNews",crawlerNews);
        LogObjectHolder.me().set(crawlerNews);
        return PREFIX + "newsMgr_edit.html";
    }

    /**
     * 获取新闻管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String,Object>> result = newsMgrDao.getAllNewsList();
        return super.warpObject(new NewsMgrWarpper(result));
    }

    /**
     * 新增新闻管理
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @Permission(Const.ADMIN_NAME)
    @BussinessLog(value = "新增新闻", key = "title", dict = Dict.NewsDict)
    @ResponseBody
    public Object add(CrawlerNews crawlerNews) {
        crawlerNews.setCreateDate(new Date());
        crawlerNews.setAuthor(ShiroKit.getUser().getName());
        //System.out.println(crawlerNews.toString());
        crawlerNews.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除新闻管理
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除新闻", key = "title,newsMgrId", dict = Dict.DeleteDict)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delete(@RequestParam Integer newsMgrId) {
        if(ToolUtil.isEmpty(newsMgrId)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.crawlerNewsMapper.deleteById(newsMgrId);
        return SUCCESS_TIP;
    }


    /**
     * 修改新闻管理
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改新闻", key = "title,info,explicitLink,link,typeId,sourcesId,placedTop,status,content,sort,label,recommend", dict = Dict.NewsDict)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object update(CrawlerNews crawlerNews) {
        if (ToolUtil.isEmpty(crawlerNews) || crawlerNews.getId() == null){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        int id = crawlerNews.getId();
        CrawlerNews temp = crawlerNewsMapper.selectById(id);
        crawlerNews.setAuthor(ShiroKit.getUser().getName());
        crawlerNews.setId(temp.getId());
        crawlerNews.setModifyDate(new Date());
        crawlerNews.setThumbUp(temp.getThumbUp());
        System.out.println(crawlerNews.toString());
        crawlerNewsMapper.updateById(crawlerNews);
        return super.SUCCESS_TIP;
    }

    /**
     * 新闻管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
